﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Crusades.Map;

namespace Crusades.MapView{

  public partial class ModernMap : IModernMap{
    public virtual List<Country> SelectableCountries { get; set; }
    public  Panel MapRoot{ get; set;}
    private double currentX;
    private double currentY;
    
    public static readonly DependencyProperty ZoomProperty
      = DependencyProperty.Register(
        "Zoom",
        typeof (double),
        typeof (ModernMap),
        new PropertyMetadata(1.0,zoomPropertyChanged));

    private readonly MapController controller;

    public double Zoom{
      get { return (double) GetValue(ZoomProperty); }
      set { SetValue(ZoomProperty, value); }
    }

    private static void zoomPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){
      var view = d as ModernMap;
      if (view != null) view.SetZoom(Convert.ToDouble(e.NewValue));
    }

    public static DependencyProperty PanXProperty
      = DependencyProperty.Register(
        "PanX",
        typeof (double),
        typeof (ModernMap),
        new PropertyMetadata(0.0, PanXPropertyChanged)
        );
    private static void PanXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){
      var view = d as ModernMap;
      if (view != null) view.SetPanX(Convert.ToDouble(e.NewValue));
    }
    public double PanX{
      get{ return (double) GetValue(PanXProperty);}
      set{SetValue(PanXProperty,value);}
    }

    public static DependencyProperty PanYProperty
      = DependencyProperty.Register(
        "PanY",
        typeof (double),
        typeof (ModernMap),
        new PropertyMetadata(0.0, PanYPropertyChanged)
        );

    private static void PanYPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){
      var view = d as ModernMap;
      if (view != null) view.SetPanY(Convert.ToDouble(e.NewValue));
    }

    public double PanY{
      get{ return (double) GetValue(PanYProperty);}
      set{SetValue(PanYProperty,value);}
    }


    

    public double MapWidth{get{ return Root.Width;}}
    public double MapHeight{get{ return Root.Height;}}

    public ModernMap() {
      InitializeComponent();
      MapRoot = Root;
      controller = new MapController(this);
      
    }
    public void SetZoom(double newZoom){
      controller.Zoom(newZoom);
    }

    private void SetPanX(double newX){
      currentX = newX;
      controller.Pan(currentX, currentY);
    }


    private void SetPanY(double newY){
      currentY = newY;
      controller.Pan(currentX,currentY);
    }
  }
}